/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

package ru.teleology.cubefs.api.reflection.metadata;

import java.util.HashMap;
import java.util.Map;

import ru.teleology.cubefs.api.models.Page;
import ru.teleology.cubefs.api.reflection.NativeType;
import ru.teleology.cubefs.api.reflection.Type;
import ru.teleology.cubefs.api.reflection.TypeSystem;
import ru.teleology.cubefs.api.reflection.Types;

@NativeType(name = Types.PAGE_METADATA_TYPE)
public class PageMetadata extends MetadataGroup {

	private static final Type myType = TypeSystem.find(Types.PAGE_METADATA_TYPE);
	private Operations operations = (Operations) myType.proxy(this);
	
	private Map<Integer, FieldDescriptor> abiFieldMap 	= new HashMap<Integer, FieldDescriptor>();
	private Map<Integer, FieldDescriptor> fieldMap 	= new HashMap<Integer, FieldDescriptor>();
	
	private int fieldsCount;
	
	public PageMetadata(long handle) {
		super(handle);
		
		processGroups(this);
	}
	
	public int Hash() {
		return operations.Hash();
	}
    
	public boolean IsAbiCompatible() {
		return operations.IsAbiCompatible();
	}
	
	public int GetPageDataBlockSize(Page page) {
		return operations.GetPageDataBlockSize(page);
	}
	
	private void processGroups(MetadataGroup group) {
		int size = group.Size();
		for (int c = 0; c < size; c++) 
		{
			Metadata metadata = group.GetItem(c);
			if (metadata instanceof FieldMetadata) 
			{
				FieldMetadata field = (FieldMetadata) metadata;
				
				FieldDescriptor descriptor = new FieldDescriptor(field, fieldsCount++, group);
				
				int start = (int) field.Ptr();
				int end = (int) (start + field.Size() * field.Count());
				for (int d = start; d < end; d++) {
					fieldMap.put(d, descriptor);
				}
				
				int abi_start = (int) field.AbiPtr();
				int abi_end = (int) (abi_start + field.Size() * field.Count());
				for (int d = abi_start; d < abi_end; d++) {
					abiFieldMap.put(d, descriptor);
				}
			}
			else if (metadata instanceof MetadataGroup) {
				processGroups((MetadataGroup)metadata);
			}
		}
	}
	
	public FieldDescriptor getField(int idx, boolean abi) {
		return abi ? abiFieldMap.get(idx) : fieldMap.get(idx);
	}
	
	@Override
	public String toString() {
		return "PAGE: "+Hash();
	}

	public interface Operations {
		int Hash();
	    boolean IsAbiCompatible();
	    int GetPageDataBlockSize(Page page);
	}
	
	public class FieldDescriptor {
		private final FieldMetadata field;
		private final int index;
		private final MetadataGroup group;
		
		public FieldDescriptor(FieldMetadata field, int index, MetadataGroup group) {
			this.field = field;
			this.index = index;
			this.group = group;
		}

		public FieldMetadata getField() {
			return field;
		}

		public int getIndex() {
			return index;
		}

		public MetadataGroup getGroup() {
			return group;
		}
	}
}
